package br.progweb.webapp.util;

import org.springframework.util.DigestUtils;

/**
 * Created with IntelliJ IDEA.
 * User: luiza
 * Date: 22/08/12
 * Time: 11:23
 */
public class Gravatar {
// ------------------------------ FIELDS ------------------------------

    private final static String GRAVATAR_AVATAR_URL = "https://secure.gravatar.com/avatar/";
    private final static String GRAVATAR_PROFILE_URL = "https://secure.gravatar.com/";

// -------------------------- OTHER METHODS --------------------------

    public String getUrl(String email, GravatarURLType urlType) {
        String url;

        if (email == null)
            return "";

        // hexadecimal MD5 hash of the requested user's lowercased email address with all whitespace trimmed
        String emailHash = DigestUtils.md5DigestAsHex(email.toLowerCase().trim().getBytes());
        String params = "?s=40&d=mm";

        if (urlType.equals(GravatarURLType.AVATAR))
            url = String.format("%s%s.jpg%s", GRAVATAR_AVATAR_URL, emailHash, params);
        else
            url = String.format("%s%s", GRAVATAR_PROFILE_URL, emailHash);

        return url;
    }

// -------------------------- ENUMERATIONS --------------------------

    public enum GravatarURLType {
        AVATAR,
        PROFILE
    }
}
